<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); ?>

<?php
$attributes = array(
    'class' => 'control-label',
);
?>

<div class="span12">

    <?php echo get_message(); ?>
    <h2><?php echo $titulo; ?></h2>

    <?php echo form_open(); ?>

    <div class="row-fluid"><div class="span2">
            <div class="control-group">
                <?php echo form_label('Buscar Por:', 'buscar', $attributes); ?>
                <div class="controls">
                    <?php
                    echo form_dropdown('buscar', array(1 => 'Causa', 2 => 'Médico'), '1', 'class="span11" id="buscar"');
                    ?>
                </div>
            </div>
        </div>


        <div class="span2 causa">
            <div class="control-group">
                <?php echo form_label('Nro.', 'causaId', $attributes); ?>
                <div class="controls">
                    <?php echo form_input(array('class' => 'span11', 'name' => 'causaId', 'type' => 'text')); ?>
                </div>
            </div>
        </div>
        <div class="span2 causa">
            <div class="control-group">
                <?php echo form_label('Carátula', 'caratula', $attributes); ?>
                <div class="controls">
                    <?php echo form_input(array('class' => 'span11', 'name' => 'caratula', 'type' => 'text')); ?>
                </div>
            </div>
        </div>
        <div class="span2 causa">
            <div class="control-group">
                <?php echo form_label('Nro. Exp.', 'nroExpediente', $attributes); ?>
                <div class="controls">
                    <?php echo form_input(array('class' => 'span11', 'name' => 'nroExpediente', 'type' => 'text')); ?>
                </div>
            </div>
        </div>


        <div class="span2 medico">
            <div class="control-group">
                <?php echo form_label('Matrícula', 'matricula', $attributes); ?>
                <div class="controls">
                    <?php echo form_input(array('class' => 'span11', 'name' => 'matricula', 'type' => 'text')); ?>
                </div>
            </div>
        </div>
        <div class="span2 medico">
            <div class="control-group">
                <?php echo form_label('Documento', 'numeroDoc', $attributes); ?>
                <div class="controls">
                    <?php echo form_input(array('class' => 'span11', 'name' => 'numeroDoc', 'type' => 'text')); ?>
                </div>
            </div>
        </div>
        <div class="span2 medico">
            <div class="control-group">
                <?php echo form_label('Nombre', 'nombre', $attributes); ?>
                <div class="controls">
                    <?php echo form_input(array('class' => 'span11', 'name' => 'nombre', 'type' => 'text')); ?>
                </div>
            </div>
        </div>


        <div class="span2">
            <div class="clearfix" style="height: 25px;"></div>
            <div class="control-group">
                <div class="controls">
                    <?php echo form_input(array('class' => 'span11', 'name' => 'submit', 'type' => 'submit', 'value' => 'Buscar', 'class' => 'btn')); ?>
                </div>
            </div>
        </div>

        <?php echo form_close(); ?>

        <div class="clearfix"></div>

        <?php
        if (!empty($busqueda)) {
            ?>
            <table class="table-striped" style="width: 100%">
                <thead>
                    <tr>
                        <th>Causa Id</th>
                        <th>Carátula</th>
                        <th>Nro. Exp.</th>
                        <th>Matrícula</th>
                        <th>Nombre</th>
                        <th>Nro. Doc.</th>
                        <th>Defendido</th>
                        <th>Subsidiado</th>
                        <th></th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    if (!empty($busqueda)) {
                        foreach ($busqueda as $demandado) {
                            ?>
                            <tr class="<?php echo ($demandado->subsidiado == 1) ? 'success' : 'error'; ?>">
                                <td id="<?php echo $demandado->id; ?>-causaId"><?php echo $demandado->causaId; ?></td>
                                <td id="<?php echo $demandado->id; ?>-caratula"><?php echo $demandado->caratula->nombre; ?></td>
                                <td id="<?php echo $demandado->id; ?>-nroExpediente"><?php echo $demandado->caratula->nroExpediente; ?></td>
                                <td id="<?php echo $demandado->id; ?>-matricula"><?php echo $demandado->matricula; ?></td>
                                <td id="<?php echo $demandado->id; ?>-nombre"><?php echo $demandado->nombre; ?></td>
                                <td id="<?php echo $demandado->id; ?>-numeroDoc"><?php echo $demandado->numeroDoc; ?></td>
                                <td><?php echo ($demandado->defendido == 1) ? 'si' : 'no'; ?></td>
                                <td><?php echo ($demandado->subsidiado == 1) ? 'si' : 'no'; ?></td>
                                <td><button id="<?php echo $demandado->id; ?>" class="btn btn-mini agregar" type="button">Agregar</button></td>
                                <td><button type="button" onclick="javascript:estadoCuenta();" class="btn btn-mini" title="Estado Cuenta"><i class="icon icon-search"></i></button></td>
                            </tr>
                            <?php
                        }
                    }
                    ?>
                </tbody>
            </table>
            <?php
        }
        ?>

    </div>
</div>

<style>
    .table-striped tr > td {
        vertical-align: middle;
    }
    .table-striped tr.success > td {
        background-color: #dff0d8 !important;
    }
    .table-striped tr.error > td {
        background-color: #f2dede !important;
    }
    .table-striped tr.error > td {
        background-color: #f2dede !important;
    }
    .table-striped tr.info > td {
        background-color: #d9edf7 !important;
    }
</style>

<?php echo form_hidden('url', ''); ?>

<div class="clearfix"></div>

<script type="text/javascript">
    var popup;
    function estadoCuenta(){
        popup = window.open("<?php echo base_url('medicos/estado_cuenta/' . $demandado->matricula . '/' . $demandado->fechaInicio . '#acto'); ?>","_blank","left=300,top=100,width=500,height=500,scrollbars=yes");
        popup.focus();
    }
    function showCausaMedico(){
        if($('#buscar').val() == 1) {
            $('.causa').show();
            $('.medico').hide();
        } else {
            $('.causa').hide();
            $('.medico').show();
        }
    }
    
    function agregar(id){
        var ret = opener.document.getElementById('beneficiarios').innerHTML;
        ret += '<tr id="' + id + '-eliminar">';
        ret += '<td>' + document.getElementById(id + '-causaId').innerHTML + '</td>';
        ret += '<td>' + document.getElementById(id + '-caratula').innerHTML + '</td>';
        ret += '<td>' + document.getElementById(id + '-nroExpediente').innerHTML + '</td>';
        ret += '<td>' + document.getElementById(id + '-matricula').innerHTML + '</td>';
        ret += '<td>' + document.getElementById(id + '-nombre').innerHTML + '</td>';
        ret += '<td>' + document.getElementById(id + '-numeroDoc').innerHTML + '</td>';
        ret += '<td><input type="text" name="importes[]" class="span11" value="0.0" /><input type="hidden" name="beneficiarios[]" value="' + id + '" /></td>';
        ret += '<td><a class="btn btn-small borrar" href="#borrar" role="button" data-toggle="modal" dbid="' + id + '"><span class="icon-remove"></span> Borrar</a></td>';
        ret += '</tr>';
        opener.document.getElementById('beneficiarios').innerHTML = ret;
    }
    
    $(document).ready(function(){
        showCausaMedico();
        
        $('#buscar').change(function() {
            showCausaMedico();
        });
        
        $('.agregar').click(function() {
            agregar($(this).attr('id'));
            $(this).parent().parent().addClass('info');
            $(this).hide();
        });
    });
    
</script>